//
//  StatusViewModel.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/21.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

//间距
/// 将相关的布局宽度数据包装到一个结构体中
struct CellLayout {
    
    //视图间隔
    let seprator: CGFloat = 10
    
    //图片视图宽度
    let pictureViewWidth: CGFloat
    
    //单张图片宽度
    let singlePictureWidth: CGFloat
    
    //头像视图的size
    let userAvatarSize: CGSize = CGSizeMake(36, 36)
    
    init() {
        
        //屏幕宽度-两个视图间隔
        pictureViewWidth = screenWidth - seprator * 2
        
        //图片视图的宽度，减去两个视图间隔，除以3
        singlePictureWidth = (pictureViewWidth - seprator * 2) / 3
    }
}

//定义一个布局结构体常量
let cellLayout = CellLayout()

class StatusViewModel: NSObject {
    
    /// 微博的数据模型
    var status: Status
    
    /// 会员等级图片
    var levelIconImage: UIImage?
    
    /// vip认证图片
    var vipIconImage: UIImage?
    
    /// 来源文字
    var sourceText: String?
    
    /// 发布时间
    var timeText: String?
    
    /// 配图视图大小
    var pictureViewSize: CGSize = CGSizeZero
    
    /// 转发微博的配图视图的大小
    var retweetedPictureViewSize: CGSize = CGSizeZero
    
    /// 转发微博文字
    var retweetedText: String?
    
    init(status: Status) {
        
        self.status = status
        super.init()
        
        //处理用户数据：会员等级，认证类型
        createUserData()
        
        //处理微博数据：来源，时间
        createStatusData()
        
        //处理转发微博数据
        createRetweetedData()
        
        //计算原创微博的配图视图size
        pictureViewSize = caculatePictureViewSize(status.pic_urls)
        
        //计算转发微博的配图视图的size
        retweetedPictureViewSize = caculatePictureViewSize(status.retweeted_status?.pic_urls)
    }
    
    /// 处理转发微博的数据
    func createRetweetedData() {
        
        guard let retweeted_status = status.retweeted_status else {
            
            return
        }
        
        //处理转发微博文字
        retweetedText = "@\((retweeted_status.user?.screen_name)!):\(retweeted_status.text!)"
    }
    
    ///  计算图片视图的size
    ///
    ///  - returns: 图片视图的size
    func caculatePictureViewSize(pictureModels: [PictureModel]?) ->CGSize {
        
        if let pictureModels = pictureModels where pictureModels.count > 0 {
            
            let count = pictureModels.count
            
            //分页算法，算出当前多少行
            let row = (count - 1) / 3 + 1
            
            //根据行数，和单行高度，算出图片视图的高度
            let pictureViewHeight = cellLayout.singlePictureWidth * CGFloat(row) + CGFloat(row - 1) * cellLayout.seprator
            
            return CGSizeMake(cellLayout.pictureViewWidth, pictureViewHeight)
            
        } else {
            
            //如果没有图片，则返回CGRectZero
            return CGSizeZero
        }
    }
    
    ///  处理用户信息数据
    func createUserData() {
        
        //处理会员等级图像
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            
//            print("common_icon_membership_level\(status.user?.mbrank)")
            
            if let mbrank = status.user?.mbrank {
                
                levelIconImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            }
        }
        
        //处理认证类型图像
        // 2.认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
        if let verified_type = status.user?.verified_type {
            
            switch verified_type {
                
                case 0:
                    self.vipIconImage = UIImage(named: "avatar_vip")
                case 2, 3, 5:
                    self.vipIconImage = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    self.vipIconImage = UIImage(named: "avatar_grassroot")
                    
                default: break
            }
        }
    }
    
    ///  处理微博信息数据
    func createStatusData() {
        
        //处理来源字符串
        if let source = status.source {
            
            let startIndex = source.rangeOfString("\">")?.endIndex
            let endIndex = source.rangeOfString("</a>")?.startIndex
            
            if let startIndex = startIndex, endIndex = endIndex {
                
                sourceText = source.substringWithRange(startIndex..<endIndex)
            }
        }
        
        //处理时间字符串
        let sinadate = NSDate.getDateFromSina(status.created_at!)
        timeText = sinadate.sina_description
    }
}
